//package com.np.rtmpdump6;
//
//import androidx.annotation.NonNull;
//import androidx.appcompat.app.AppCompatActivity;
//
//import android.Manifest;
//import android.app.Activity;
//import android.content.Context;
//import android.content.Intent;
//import android.content.pm.PackageManager;
//import android.hardware.display.VirtualDisplay;
//import android.media.AudioManager;
//import android.media.MediaCodec;
//import android.media.MediaCodecInfo;
//import android.media.MediaFormat;
//import android.media.MediaPlayer;
//import android.media.projection.MediaProjection;
//import android.media.projection.MediaProjectionManager;
//import android.net.Uri;
//import android.opengl.GLSurfaceView;
//import android.os.Build;
//import android.os.Bundle;
//import android.view.Surface;
//import android.view.SurfaceHolder;
//import android.view.SurfaceView;
//import android.view.View;
//
//import java.io.IOException;
//
//public class MainActivity_Backup extends AppCompatActivity {
//
//    private MediaPlayer mediaPlayer1;
//    private SurfaceView surfaceView1;
//    private MediaProjectionManager mediaProjectionManager;
//    private MediaProjection mediaProjection;
//    private ScreenLive screenLive;
//    private String url = "rtmp://192.168.2.156:10035/live/i6YJfdHSg?sign=ieL1BOHIgz";
//    //private String url = "rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_3546379957831915_58459438&key=56cc8bc5a338f4c3d9c5da8a46fe501a&schedule=rtmp&pflag=1";
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//        surfaceView1 = findViewById(R.id.surfaceView1);
//        checkPermission();
//        loadVideo();
//
//        findViewById(R.id.bPush).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
//                screenLive = new ScreenLive();
//                screenLive.startLive(url, mediaProjection);
//            }
//        });
//
//        try {
//
//            MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, 720, 1280);
//            format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
//            format.setInteger(MediaFormat.KEY_BIT_RATE, 400_000);
//            format.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
//            format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 2);
//
//            {
//                // 创建MediaCodec对象
//                MediaCodec codec = MediaCodec.createEncoderByType("video/avc");
//
//                // 配置编码器
//                codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
//
//                // 创建SurfaceView对象并获取其Surface对象
//                Surface surface = surfaceView1.getHolder().getSurface();
//
//                // 将Surface对象设置为MediaCodec的输入端口
//                codec.setInputSurface(surface);
//
//                // 启动编码器
//                codec.start();
//            }
//
////            {
////                MediaCodec codec = MediaCodec.createEncoderByType("video/avc");
////                codec.configure(format, surfaceView1.getHolder().getSurface(), null, MediaCodec.CONFIGURE_FLAG_ENCODE); //The surface has been released
////            }
////            {
////                MediaCodec codec = MediaCodec.createEncoderByType("video/avc");
////                codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
////                Surface surface = surfaceView1.getHolder().getSurface();
////                codec.setInputSurface(surface); //这句报错 not a PersistentSurface
////            }
////            {
////                MediaCodec codec = MediaCodec.createEncoderByType("video/avc");
////                codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
////                Surface surface = MediaCodec.createPersistentInputSurface();
////                codec.setInputSurface(surface);
////            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
//            //mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
//            screenLive = new ScreenLive();
//            //screenLive.startLive(url, mediaProjection);
//            screenLive.startLive(url, surfaceView1);
//        }
//    }
//
//    /**
//     * 加载视频
//     */
//    private void loadVideo() {
//        mediaPlayer1 = new MediaPlayer();
//        surfaceView1.getHolder().addCallback(new SurfaceHolder.Callback() {
//            @Override
//            public void surfaceCreated(@NonNull SurfaceHolder holder) {
//                //String path = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
//                String path = "/storage/emulated/0/storage/emulated/0/Android/ultrasound.mp4";
//
//                try {
//                    mediaPlayer1 = new MediaPlayer();
//                    mediaPlayer1.reset();
//                    mediaPlayer1.setAudioStreamType(AudioManager.STREAM_MUSIC);
//                    mediaPlayer1.setDataSource(MainActivity_Backup.this, Uri.parse(path));
//                    mediaPlayer1.setDisplay(surfaceView1.getHolder());
//                    mediaPlayer1.prepare();
//                    mediaPlayer1.start();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//
//            @Override
//            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
//
//            }
//
//            @Override
//            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
//
//            }
//        });
//    }
//
//    public void startLive(View view) {
////        this.mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
////        Intent captureIntent = mediaProjectionManager.createScreenCaptureIntent();
////        startActivityForResult(captureIntent, 100);
//
//        //startActivityForResult(null,100);
//    }
//
//    public boolean checkPermission() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
//                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//            requestPermissions(new String[]{
//                    Manifest.permission.READ_EXTERNAL_STORAGE,
//                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                    Manifest.permission.CAMERA
//            }, 1);
//        }
//        return false;
//    }
//
//    public void stopLive(View view) {
//    }
//}